package com.gc.asm.client.exception;

import com.alibaba.fastjson.JSONObject;
import com.gc.asm.client.CommonUtil;
import com.gc.asm.client.constans.ConstantsEnum;
import org.apache.commons.lang3.StringUtils;

public class CommonJsonException extends RuntimeException {

    private JSONObject resultJson;

    public CommonJsonException(String returnCode, String returnMsg) {
        this.resultJson = CommonUtil.errorJson(returnCode, returnMsg);
    }

    public CommonJsonException(JSONObject resultJson) {
        this.resultJson = resultJson;
    }

    /**
     * 转换API返回信息
     * @param returnMsg
     * @return
     */
    public static String convertApiReturnMsg(String returnMsg) {
        if(StringUtils.isNotEmpty(returnMsg)){
            if (returnMsg.indexOf(ConstantsEnum.API_AUTH_INTERFACE_INVALID.getKey()) > -1) {
                return ConstantsEnum.API_AUTH_INTERFACE_INVALID.getValue();
            } else if (returnMsg.indexOf(ConstantsEnum.API_REQUEST_NUMBER_EXCEEDED.getKey()) > -1) {
                return ConstantsEnum.API_REQUEST_NUMBER_EXCEEDED.getValue();
            } else if (returnMsg.indexOf(ConstantsEnum.API_AUTH_INVALID.getKey()) > -1) {
                return ConstantsEnum.API_AUTH_INVALID.getValue();
            } else if (returnMsg.indexOf(ConstantsEnum.API_AUTH_IS_NULL.getKey()) > -1) {
                return ConstantsEnum.API_AUTH_IS_NULL.getValue();
            }else if (returnMsg.indexOf(ConstantsEnum.API_REQUEST_IP_IS_NULL.getKey()) > -1) {
                return ConstantsEnum.API_REQUEST_IP_IS_NULL.getValue();
            } else if (returnMsg.indexOf(ConstantsEnum.API_AUTH_IP_INVALID.getKey()) > -1) {
                return ConstantsEnum.API_AUTH_IP_INVALID.getValue();
            }
        }
        return returnMsg;
    }
}
